﻿@{
    Layout = "";
}
@model Nop.Plugin.DiscountRules.HasAllProducts.Models.RequirementModel
@using Nop.Web.Framework;

    <script type="text/javascript">
        $(document).ready(function () {
            $('#saveHasAllProductsrequirement@(Model.RequirementId)').click(function () {
                var productIds = $("#@Html.FieldIdFor(model => model.Products)").val();
                var discountId = @Model.DiscountId;
                var requirementId = @Model.RequirementId;
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("Configure", "DiscountRulesHasAllProducts"))",
                    data: { "discountId": discountId, "discountRequirementId": requirementId, "productIds": productIds },
                    success: function (data) {
                         $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                         //notify parent if it's a new requirement
                         @if (Model.RequirementId == 0)
                         {
                            <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                         }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('@Html.Raw(HttpUtility.JavaScriptStringEncode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                    }  
                });
            });
        });
    </script>
    
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Products):
            </td>
            <td class="adminData">
                <div>@Html.EditorFor(model => model.Products) <input type="button" id="saveHasAllProductsrequirement@(Model.RequirementId)" class="k-button" value="@T("Admin.Common.Save")" /></div>
                <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display:none;">@T("Admin.Promotions.Discounts.Requirements.Saved")</div>

                <span id="discountrequirement-products-check-progress@(Model.RequirementId)" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
                <input type="submit" id="btnAddNewDiscountRequirementProduct@(Model.RequirementId)" value="@T("Plugins.DiscountRules.HasAllProducts.Fields.Products.AddNew")" onclick="javascript:OpenWindow('@(Url.RouteUrl("Plugin.DiscountRules.HasAllProducts.ProductAddPopup", new { btnId = "btnRefreshDiscountRequirementProducts" + Model.RequirementId, productIdsInput = Html.FieldIdFor(model => model.Products) }))', 800, 800, true); return false;" class="k-button" />
                <input type="submit" id="btnRefreshDiscountRequirementProducts@(Model.RequirementId)" style="display: none" />
                <script type="text/javascript">
                    $(document).ready(function () {
                        $('#btnRefreshDiscountRequirementProducts@(Model.RequirementId)').click(function () {
                            //refresh product list
                            loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();

                            //return false to don't reload a page
                            return false;
                        });
                    });

                    $(document).ready(function () {
                        loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();
                    });
                    $(document).ready(function () {
                        $('#@Html.FieldIdFor(model => model.Products)')
                        .data('timeout', null)
                        .keyup(function() {
                            clearTimeout($(this).data('timeout'));
                            //one second timeout
                            $(this).data('timeout', setTimeout(loadDiscountRequirementProductFriendlyNames@(Model.RequirementId), 1000));
                        });
                    });
                    function loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)() {
                        var inputValue = $('#@Html.FieldIdFor(model => model.Products)').val();
                        if (inputValue) {
                            //load friendly names
                            $('#discountrequirement-products-check-progress@(Model.RequirementId)').show();
                            $.ajax({
                                cache: false,
                                type: 'POST',
                                url: '@Url.RouteUrl("Plugin.DiscountRules.HasAllProducts.LoadProductFriendlyNames")',
                                data: { "productIds": inputValue },
                                dataType: 'json',
                                success: function(data) {
                                    $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                    $('#discountrequirement-product-names@(Model.RequirementId)').text(data.Text);
                                },
                                failure: function () {
                                    $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                    $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                                }
                            });
                        } else {
                            $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                        }
                    }
                </script>
                <span id="discountrequirement-product-names@(Model.RequirementId)"></span>
            </td>
        </tr>
    </table>